<template>
    <div class="banner-detail" v-loading="loading">
        <el-form
            label-position="left"
            :model="detail"
            :rules="rules"
            ref="detail"
            :hide-required-asterisk="true"
            label-width="70px"
            class="form-box input-no-border-box">

            <el-form-item label="标题" prop="title">
                <el-input v-model="detail.title" maxlength="10" placeholder="最多10个字"></el-input>
            </el-form-item>

            <el-form-item label="banner" prop="bannerUrl">
                <UploadImg v-model="detail.bannerUrl" :width="1032" :height="300"/>
            </el-form-item>

            <el-form-item label="关联url" prop="route">
                <el-input v-model="detail.route" placeholder="如：qugame://game.com/app"></el-input>
            </el-form-item>

            <el-form-item label="投放时间">
                <el-date-picker
                    v-model="detail._time"
                    @change="dateChange"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
        </el-form>

        <div class="plat-wrap">
            <div class="plat-list">
                <div class="label" style="width: 86px">平台：</div>
                <div class="f-list">
                    <el-checkbox-group v-model="checkListModal" @change="checkboxChange">
                        <el-checkbox v-for="(item, idx) in checkList" :key="idx" :label="item.name"></el-checkbox>
                    </el-checkbox-group>
                </div> 
            </div>
            <div class="version-input">
                <div class="v-list">
                    <span>安卓兼容版本：</span>
                    <el-input v-model="detail.androidVersion" placeholder="请输入要兼容的版本" @input="azInput" />
                </div>
                <div class="v-list">
                    <span>IOS兼容版本：</span>
                    <el-input v-model="detail.iosVersion" placeholder="请输入要兼容的版本" @input="iosInput" />
                </div>
            </div> 
        </div>

        <div class="tools">
            <el-button @click="goBack" class="btn">返回</el-button>
            <el-button type="success" class="btn save-btn" @click="saveBanner" v-if="detail.id">保存</el-button>
            <el-button type="success" class="btn save-btn" @click="saveBanner" v-else>新建</el-button>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'

export default {
    data () {
        return {
            detail: {
                bannerUrl: '',
                endTime: '',
                id: '',
                route: '',
                startTime: '',
                status: '',
                title: '',
                _time: [],
                jrVersion: '', // 兼容版本

                androidVersion: 0,
                iosVersion: 0,
                androidShow: 0,
                iosShow: 0,
                dhqShow: 0,
                dhqH5Show: 0,
                androidMjShow: 0,
            },
            loading: false, // 详情加载loading
            rules: {
                title: [
                    { required: true, message: '请填写标题', trigger: 'blur' }
                ],
                bannerUrl: [
                    { required: true, message: '请上传banner', trigger: 'change' }
                ],
                route: [
                    { required: true, message: '请选择关联url', trigger: 'blur' }
                ]
            },
            checkListModal: [],
            checkList: [
                {
                    name: '安卓 App',
                    version: 0,
                    show: 0
                },
                {
                    name: 'ios App',
                    version: 0,
                    show: 0
                },
                {
                    name: '安卓独立登号器',
                    version: 0,
                    show: 0
                },
                {
                    name: 'ios独立登号器H5',
                    version: 0,
                    show: 0
                },
                {
                    name: '安卓马甲包',
                    version: 0,
                    show: 0
                }
            ]
        }
    },

    mounted () {
        if (this.$route.query.id) {
            this.getDetail()
        } else {
            this.dataInit(this.detail)
        }
    },

    methods: {
        // 选中平台复选框
        checkboxChange(val) {
            this.checkListModal = val
            if (!this.checkListModal.includes('安卓 App')) {
                this.detail.androidVersion = 0
            }
            if (!this.checkListModal.includes('ios App')) {
                this.detail.iosVersion = 0
            }
        },
        azInput(val) {
            this.detail.androidVersion = val
        },
        iosInput(val) {
            this.detail.iosVersion = val
        },
        dateChange(val) {
            if (!val) {
                this.detail._time = []
            }
        },
        // 选中平台的参数转化
        updateParams() {
            this.checkList.forEach((list) => {
                this.$set(list, 'show', 0)
                this.checkListModal && this.checkListModal.forEach((check) => {
                    if (list.name === check) {
                        this.$set(list, 'show', 1)
                    }
                })
            })
            this.detail.androidShow = this.checkList[0].show
            this.detail.iosShow = this.checkList[1].show
            this.detail.dhqShow = this.checkList[2].show
            this.detail.dhqH5Show = this.checkList[3].show
            this.detail.androidMjShow = this.checkList[4].show
        },

        // 获取详情
        async getDetail () {
            this.loading = true
            let params = {
                id: this.$route.query.id
            }
            let res = await this.$http.get('/trade/selectBanner', {
                params
            })
            if (res.code === '0' && res.data && res.data.length) {
                this.dataInit(res.data[0])
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        // 初始化数据
        dataInit (obj) {
            if (obj.startTime && obj.endTime) {
                obj._time = [obj.startTime, obj.endTime]
            } else {
                obj._time = []
            }
            this.checkListModal = []
            this.checkList[0].show = obj.androidShow
            this.checkList[1].show = obj.iosShow
            this.checkList[2].show = obj.dhqShow
            this.checkList[3].show = obj.dhqH5Show
            this.checkList[4].show = obj.androidMjShow
            // 填充复选框
            this.checkList.forEach((item) => {
                if (item.show) {
                    this.checkListModal.push(item.name)
                }
            })

            this.detail = obj
            this.loading = false
        },

        // 返回上一页
        goBack () {
            this.$router.go(-1)
        },

        // 保存推荐
        saveBanner () {
            this.updateParams()
            this.$refs.detail.validate((valid) => {
                if (valid) {
                    this.saveBannerFn()
                } else {
                    return false
                }
            })
        },

        async saveBannerFn () {
            if (this.loading) return
            let params = JSON.parse(JSON.stringify(this.detail))
            console.log(params._time, '_time')
            if ((params._time && !params._time.length) || !params._time) {
                this.$message({
                    type: 'error',
                    message: '请选择投放时间'
                })
                return
            }
            
            if (params._time && params._time.length >= 2) {
                params.startTime = params._time[0] ? (formatDate(params._time[0], 'YYYY-MM-DD') + ' 00:00:00') : ''
                params.endTime = params._time[1] ? (formatDate(params._time[1], 'YYYY-MM-DD') + ' 23:59:59') : ''
            }
            delete params._time
            delete params.createTime
            delete params.updateTime
            delete params.status
            
            this.loading = true
            let res = await this.$http.post('/trade/updateBanner', params)
            if (res.code === '0') {
                let message
                if (params.id) {
                    message = '修改成功！'
                } else {
                    message = '保存成功！'
                }
                this.$message({
                    type: 'success',
                    message
                })
                this.dataInit(res.data)
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        }
    }
}
</script>

<style lang="scss" scoped>
    .form-box {
        max-width: 500px;
    }
    .plat-wrap {
        display: flex;
        justify-content: flex-start;
        .plat-list {
            display: flex;
            justify-content: flex-start;
            margin-bottom: 10px;
            .label {
                width: 100px;
                font-size: 14px;
                color: #606266;
            }
            
            /deep/ .el-checkbox {
                display: block;
                margin-bottom: 25px;
                margin-top: 12px;
            }
        }
        .version-input {
            margin-left: 20px;
            margin-bottom: 10px;
            .v-list {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                margin-bottom: 10px;
            }
            span {
                display: inline-block;
                min-width: 120px;
                font-size: 14px;
                color: #606266;
                font-weight: 500;
            }
        }
    }
    

    .tools {
        margin-top: 20px;
        padding-bottom: 20px;
        display: flex;
        justify-content: center;
        .save-btn {
            margin-left: 200px;
        }
        .btn {
            width: 150px;
            padding: 10px;
        }
    }
</style>
